using System;
using System.Collections.Generic;
using System.Text;
using DCI6;
using DCI6.Client.API;

namespace DCIAPIRMSampleCode {
    internal class SubscriptionSampleCode {
        public Subscription CreateSubscription() {
            /**
             * In order to create subscription user has an option to input 4 subscription parameters: 
             * 1.Meta data filtering query that corresponds to
             * subscribe/metadataFilter/xpath field in dds.xsd . Meta data filtering can contain a number
             * of different conditions that can be joined together and converted into xpath statement
             * by DCI
             * 2. Content filtering query that corresponds to subscribe/contentFilter/xpath field in
             * dds.xsd. Content filtering can contain a number
             * of different conditions that can be joined together and converted into xpath statement
             * by DCI
             * 3. Distribution type that corresponds to subscribe/policy/rule field in dds.xsd. Setting
             * this field will determine whether subscription is GLOBAL or LOCAL to that particular DDS node.
             * 4. Compression type that corresponds to subscribe/policy/controlPolicy field in dds.xsd.
             * The appropriate values for this field as per dds.xsd are: NONE
             * GZIP
             * FI
             * GFI
             * 
             * Additionally, user must provide an AreaOfInterest that corresponds to subscribe/areaOfInterest
             * field in dds.xsd
             *       
             */

            /**
             * Creating Area of Interest
             */
            AreaOfInterest aoi = AreaOfInterest.GetRectangleAOI(-90, -180, 90, 180);

            /**
             * Creating a holder object that will contain conditions for meta data filtering.
             */
            List<Criterion> metadata = new List<Criterion>();

            /**
             * Populating with different subscription criteria 
             */
            metadata.Add(Query.HasCategory("POS-RPT"));
            metadata.Add(Query.HasKeyword("MCS"));
            metadata.Add(Query.HasKeyword("FBCB2"));
            metadata.Add(Query.DescriptionEquals("PASS"));
            metadata.Add(Query.HasClassification(Classification.UNCLASSIFIED));
            metadata.Add(Query.OrganizationEquals("ARMY"));
            metadata.Add(Query.TopicTitleEquals("POS-RPT:USARMY"));

            /**
             * Constructing a meta data query object from the list of entered criteria
             */
            Query metadataQuery = new Query(Query.And(metadata.ToArray()));

            /**
             * Setting distribution field on the query object
             */
            metadataQuery.Distribution = Advertisement.Distribution.LOCAL;

            /**
             * Setting Compression field on the query object
             */
            metadataQuery.Compression = CompressionType.NONE;

            /**
             * Creating a holder object that will contain conditions for content filtering.
             */

            List<Criterion> content = new List<Criterion>();

            /**
             * Populating with different content filtering criteria 
             */
            content.Add(Query.HasKeyword("AMDWS"));
            /**
             * DCI provides convenient method to filter out different fields.
             */
            content.Add(Query.LacksKeyword("MCS"));

            /**
             * Constructing a content filtering query object from the list of entered criteria
             */
            Query contentQuery = new Query(Query.And(content.ToArray()));


            /**
             * Creating subscription object
             */
            Subscription sub = new Subscription(metadataQuery, contentQuery,
                aoi);

            return sub;
        }

        public Subscription CreateSimpleSubForAdv(Advertisement ad) {
            Query q = new Query(Query.And(Query.HasCategory(ad.Categories[0]), Query.HasKeyword("FBCB2")));
            Subscription sub = new Subscription(q, AreaOfInterest.GLOBAL);
            return sub;
        }

        public void Subscribe(Subscription subscription, String ddsConfigConnectionID) {
            /**
             * Calling subscribe() on the Subscription Object will send the 
             * Subscribe Request to the DDS Configured with the connectionID
             * whose value is equal to the value stored in ddsConfigConnectionID.
             * If the DCI Service does not currently have the DDSConnectionID
             * registered an Exception will be thrown. If the call is successfull 
             * the Subscription will be created on DDS and will be registered with
             * DCI's resubscription service which will restore the subscription
             * to DDS if it is removed by some other source. If you wish to receive data
             * for this subscription it is important you register a callback handler first.
             */
            subscription.subscribe(ddsConfigConnectionID);
            Console.WriteLine("Created Subscription: " + subscription.SubscriptionId);
        }

        public void UnSubscribe(Subscription subscription, String ddsConfigConnectionID) {
            /**
             * Calling unsubscribe() on the Subscription Object will send the 
             * UnSubscribe Request to the DDS Configured with the connectionID
             * whose value is equal to the value stored in ddsConfigConnectionID.
             * If the DCI Service does not currently have the DDSConnectionID
             * registered an Exception will be thrown. If the call is successfull 
             * the Subscription will be removed on DDS as well as from resubscription
             * monitoring. 
             */
            subscription.unsubscribe(ddsConfigConnectionID);
        }
    }
}